package com.moco.launcher.fragment;

import java.util.HashMap;

import android.annotation.SuppressLint;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.moco.launcher.R;
import com.oazon.moco.link.core.LinkSDK;

public class VolumeFragment extends BaseFragment {
	private static final String TAG = "VolumeFragment";
	private ImageView mVolume = null;
	private ImageView mExitView = null;
	AudioManager mAudioManager = null;
	private static final int FADE_OUT = 1;

	int max;// 最大音量
	@SuppressLint("UseSparseArrays")
	HashMap<Integer, Integer> mVolumes = new HashMap<Integer, Integer>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG, "onCreate");
		max = 31;
		mVolumes.clear();
		mVolumes.put(0, R.drawable.v0);
		mVolumes.put(1, R.drawable.v1);
		mVolumes.put(2, R.drawable.v2);
		mVolumes.put(3, R.drawable.v3);
		mVolumes.put(4, R.drawable.v4);
		mVolumes.put(5, R.drawable.v5);
		mVolumes.put(6, R.drawable.v6);
		mVolumes.put(7, R.drawable.v7);
		mVolumes.put(8, R.drawable.v8);
		mVolumes.put(9, R.drawable.v9);
		mVolumes.put(10, R.drawable.v10);
		mVolumes.put(11, R.drawable.v11);
		mVolumes.put(12, R.drawable.v12);
		mVolumes.put(13, R.drawable.v13);
		mVolumes.put(14, R.drawable.v14);
		mVolumes.put(15, R.drawable.v15);
		mVolumes.put(16, R.drawable.v16);
		mVolumes.put(17, R.drawable.v17);
		mVolumes.put(18, R.drawable.v18);
		mVolumes.put(19, R.drawable.v19);
		mVolumes.put(20, R.drawable.v20);
		mVolumes.put(21, R.drawable.v21);
		mVolumes.put(22, R.drawable.v22);
		// mVolumes.put(69, R.drawable.v23);
		mVolumes.put(23, R.drawable.v24);
		mVolumes.put(24, R.drawable.v25);
		mVolumes.put(25, R.drawable.v26);
		mVolumes.put(26, R.drawable.v27);
		mVolumes.put(27, R.drawable.v28);
		mVolumes.put(28, R.drawable.v29);
		mVolumes.put(29, R.drawable.v30);
		mVolumes.put(30, R.drawable.v31);
		mVolumes.put(31, R.drawable.v32);
	}

	@SuppressLint("HandlerLeak")
	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case FADE_OUT:
				if (ml != null) {
					ml.finishActivity();
				}
				break;
			default:
				break;
			}
		}
	};

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.volume_layout, container,
				false);
		mVolume = (ImageView) rootView.findViewById(R.id.volume);
		mExitView = (ImageView) rootView.findViewById(R.id.exit);
		mExitView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Log.d(TAG, "onClick..........");
				if (ml != null) {
					ml.finishActivity();
				}
			}
		});
		rootView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Log.d(TAG, "onClick..........");
				if (ml != null) {
					ml.finishActivity();
				}
			}
		});
		adjustVolumeProgree(LinkSDK.curVolume);

		return rootView;
	}

	public void adjustVolumeProgree(int i) {
		int prog = i;
		try {
			Log.e(TAG, "====" + prog);
			mVolume.setImageResource(this.mVolumes.get(prog).intValue());
			mVolume.setAlpha(0.8f);
		} catch (Exception e) {
			Log.e(TAG, "prog++out" + prog);
		}

		mHandler.removeMessages(FADE_OUT);
		mHandler.sendEmptyMessageDelayed(FADE_OUT, 2000);
	}

}
